package com.lintcode.solution;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Solution_46_47 {
    public static void main(String[] agrs) {
        Integer[] i = { 1, 1, 1, 1, 2, 2, 2 };
        List<Integer> list = Arrays.asList(i);
        Solution_46_47 solution = new Solution_46_47();
        System.out.println(solution.majorityNumber(list));
    }
    
    public int majorityNumber(List<Integer> nums) {
        int count = 0, result = nums.get(0);
        Map<Integer, Integer> map = new HashMap<>();
        for (Integer num : nums) {
            map.merge(num, 1, (a, b) -> a + b);
        }
        for (Integer key : map.keySet()) {
            if (count < map.get(key)) {
                count = map.get(key);
                result = key;
            }
        }
        return result;
    }
}
